var _ = require('lodash');
var should = require('should');
var analyze = require('kc-analyze');
var equipHandler = require('../../../../app/servers/bag/handler/equipHandler');
var config = require('../../../config');
var bagModel = require('../../../../app/models/bagModel');
var mongoConnector = require('../../../../app/libs/mongodb/mongoConnector');
var configUtil = require('../../../../app/util/configUtil');
var Bag = mongoConnector().model('Bag');

suite('初始化', function () {
    test('数据统计', function () {
        var analyze_config = configUtil.load('analyze');
        var game_config = configUtil.load('game');
        analyze.initialize({
            host: analyze_config.host,
            server_id: game_config.server_id
        });
    });
});

suite('forge', function () {
    var bag_model = null;
    var equip_type = 1111000;
    var next_equip_type = 1112000
    var equip_id = 0;
    setup(function (done) {
        Bag.remove({
            role_id: config.other_role_id
        }, function (err) {
            if (err) {
                return handleError(err);
            }
            bagModel.getByRoleId(config.other_role_id, function (err, model) {
                bag_model = model;
                bag_model.addEquip(equip_type, true);
                bag_model.addEquip(next_equip_type, true);
                bag_model.addProperty(3001101, 23, true);
                bag_model.addProperty(3001102, 17, true);
                bag_model.save(done);
            });
        });
    });

    test('铸造装备(正常)', function (done) {
        var msg = {};
        var session = {};
        session.uid = config.other_role_id;

        var equip_list = bag_model.getEquipList();
        var equip = _.find(equip_list, {
            equip_type: equip_type
        });
        equip_id = equip.getId();
        msg.equip_id = equip_id;

        var equip_handler = new equipHandler();
        equip_handler.forge(msg, session, function (err, res) {
            console.log('res:', res);
            res.code.should.eql(200);
            done();
        });
    });

    test('铸造装备(无效装备)', function (done) {
        var msg = {};
        var session = {};
        session.uid = config.other_role_id;

        msg.equip_id = 123123;

        var equip_handler = new equipHandler();
        equip_handler.forge(msg, session, function (err, res) {
            //console.log('res:',res);
            res.code.should.eql(6001);
            done();
        });
    });

    test('铸造装备(材料不足)', function (done) {
        var msg = {};
        var session = {};
        session.uid = config.other_role_id;

        var equip_list = bag_model.getEquipList();
        var equip = _.find(equip_list, {
            equip_type: next_equip_type
        });
        msg.equip_id = equip.getId();

        var equip_handler = new equipHandler();
        equip_handler.forge(msg, session, function (err, res) {
            //console.log('res:',res);
            res.code.should.eql(6010);
            done();
        });
    });
});
